package com.yyj.web.controller.manager;

import com.yyj.common.BaseResponse;
import com.yyj.constant.ReturnCode;
import com.yyj.constant.WebConstant;
import com.yyj.model.TPropertyManager;
import com.yyj.pojo.user.SessionInfo;
import com.yyj.service.ManagerService;
import com.yyj.service.UserService;
import com.yyj.util.RspUtils;
import com.yyj.web.controller.BaseController;
import com.yyj.web.protocol.req.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Api(value = "管理员登录和管理相关接口" , description="管理员登录和管理相关接口")
@RestController
public class ManagerController extends BaseController {

    @Autowired
    ManagerService managerService;

    @Autowired
    UserService userService;

    @RequestMapping(value = "/managers/testLogin" , method = RequestMethod.POST)
    public BaseResponse userLogin(
            String mobile , HttpSession session
    ){
        try{
            TPropertyManager propertyOwner = managerService.findManagerByMobile(mobile);
            SessionInfo sessionInfo = new SessionInfo();
            BeanUtils.copyProperties(propertyOwner , sessionInfo);
            sessionInfo.setAdmin(true);
            sessionInfo.setIsRoot(propertyOwner.getIsRoot());
            session.setAttribute(WebConstant.SESSION_INFO, sessionInfo);
            return RspUtils.success(sessionInfo);
        }catch(Exception e){
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询当前管理员信息（是否超管）")
    @RequestMapping(value = "/auth/managers/info" , method = RequestMethod.GET)
    public BaseResponse userLogin(
    ){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            return RspUtils.success(sessionInfo);
        }catch(Exception e){
            logger.error("异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "管理员查询对应房号下的业主信息列表")
    @RequestMapping(value = "/auth/managers/ownersList/{roomNum}" , method = RequestMethod.GET)
    public BaseResponse ownersList(@PathVariable String roomNum
    ){
        try{
            return RspUtils.success(managerService.queryManagerOwners(roomNum));
        }catch(Exception e){
            logger.error("管理员查询对应房号下的业主信息列表异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "管理员添加业主账号")
    @RequestMapping(value = "/auth/managers/addOwner" , method = RequestMethod.POST)
    public BaseResponse addOwner(@RequestBody @Validated OwnerAddReq ownerAddReq , BindingResult result
    ){
        try{
            checkRequestParams(result);
            userService.addOwner(ownerAddReq);
            return RspUtils.success("提添加成功");
        }catch(Exception e){
            logger.error("添加业主账号异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "超级管理员添加管理员账号")
    @RequestMapping(value = "/auth/managers/addManager" , method = RequestMethod.POST)
    public BaseResponse addManager(@RequestBody @Validated ManagerAddReq managerAddReq , BindingResult result
    ){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            if(null != sessionInfo && sessionInfo.getIsRoot() != 1){
                return RspUtils.error("非超管用户不能操作");
            }
            checkRequestParams(result);
            managerService.addManager(managerAddReq);
            return RspUtils.success();
        }catch(Exception e){
            logger.error("超级管理员添加管理员账号异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "超级管理员查看管理员账号列表")
    @RequestMapping(value = "/auth/managers/list" , method = RequestMethod.GET)
    public BaseResponse addManager(
    ){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            if(null != sessionInfo && sessionInfo.getIsRoot() != 1){
                return RspUtils.error("非超管用户不能操作");
            }
            return RspUtils.success(managerService.queryManagerList());
        }catch(Exception e){
            logger.error("超级管理员查看管理员账号列表异常：{}" , e.getMessage() , e);
            return RspUtils.error(e , e.getMessage());
        }
    }

    @ApiOperation(value = "管理员查看费用设置列表")
    @RequestMapping(value = "/auth/managers/fee" , method = RequestMethod.GET)
    public BaseResponse feeList(
    ){
        try{
            return RspUtils.success(managerService.feeConfigList());
        }catch(Exception e){
            logger.error("管理员查看费用设置列表异常：{}" , e.getMessage() , e);
            return RspUtils.error(e , e.getMessage());
        }
    }

    @ApiOperation(value = "管理员修改费用设置")
    @RequestMapping(value = "/auth/managers/updateFee" , method = RequestMethod.POST)
    public BaseResponse updateFee(@RequestBody FeeUpdateReq feeUpdateReq, BindingResult result

    ){
        try{
            checkRequestParams(result);
            managerService.updateFee(feeUpdateReq.getId() , Double.valueOf(feeUpdateReq.getFee()));
            return RspUtils.success();
        }catch(Exception e){
            logger.error("管理员修改费用设置异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "超级管理员修改管理员账号状态")
    @RequestMapping(value = "/auth/managers/updateManagerStatus" , method = RequestMethod.POST)
    public BaseResponse updateManagerStatus( @RequestBody UserStatusUpdateReq userStatusUpdateReq , BindingResult result

    ){
        try{
            checkRequestParams(result);
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            if(null != sessionInfo && sessionInfo.getIsRoot() != 1){
                return RspUtils.error("非超管用户不能操作");
            }
            managerService.updateManagerStatus(userStatusUpdateReq);
            return RspUtils.success("修改成功");
        }catch(Exception e){
            logger.error("超级管理员修改管理员账号状态异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "管理员修改业主账号状态")
    @RequestMapping(value = "/auth/managers/updateOwnerStatus" , method = RequestMethod.POST)
    public BaseResponse updateOwnerStatus( @RequestBody UserStatusUpdateReq userStatusUpdateReq , BindingResult result

    ){
        try{
            checkRequestParams(result);
            userService.updateOwnerStatus(userStatusUpdateReq);
            return RspUtils.success("修改成功");
        }catch(Exception e){
            logger.error("管理员修改业主账号状态异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }


}
